/**
 * @see https://umijs.org/zh-CN/plugins/plugin-access
 * */
import { formatToTreeArr, getParentTree } from '@/utils/format';
import { isNullOrUndefined } from './utils/checkType';
export default function access(initialState: {
  authMenu: MenuManagementApi.GetRelationResponse[];
}) {
  const { authMenu = [] } = initialState;
  const treeData = formatToTreeArr(authMenu);
  const access: Record<string, boolean> = {};
  authMenu
    .filter((auth) => auth.isChoose)
    .forEach((auth) => {
      const menus = getParentTree(treeData, auth.id);
      menus.forEach((menu) => {
        const { linkName } = menu;
        if (isNullOrUndefined(access[linkName])) {
          access[linkName] = true;
        }
      });
    });

  return access;
}
